﻿using Microsoft.Win32;
using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;

namespace C11_PRC_Editor
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        #region Private Fields

        private Collection<BindingExpression> be;
        private string filterForFileDialog = "prc-File (*.prc)|*.prc";

        #endregion Private Fields

        #region Public Constructors

        public MainWindow()
        {
            InitializeComponent();
            this.be = new Collection<BindingExpression>();

            this.editor.FileLoaded += editor_FileLoaded;
        }

        #endregion Public Constructors

        #region Private Methods

        private void Btnclose_Click(object sender, RoutedEventArgs e)
        {
            App.Current.Shutdown();
        }

        private void Btnmax_Click(object sender, RoutedEventArgs e)
        {
            if (this.WindowState == System.Windows.WindowState.Normal)
            {
                //this.AllowsTransparency = false;
                //this.WindowStyle = System.Windows.WindowStyle.ThreeDBorderWindow;
                this.WindowState = System.Windows.WindowState.Maximized;
                //this.WindowStyle = System.Windows.WindowStyle.None;
                //this.AllowsTransparency = true;
            }
            else
                this.WindowState = System.Windows.WindowState.Normal;
        }

        private void Btnmin_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = System.Windows.WindowState.Minimized;
        }

        private void editor_FileLoaded(object sender, EventArgs e)
        {
            be.Add(SunTextBox.GetBindingExpression(ControlTest.TextBoxTextProperty));
            be.Add(AmbExtraTextBox.GetBindingExpression(ControlTest.TextBoxTextProperty));
            be.Add(HalloText.GetBindingExpression(TextBox.TextProperty));

            foreach (var item in be)
            {
                item.UpdateTarget();
            }

            be.Clear();
        }

        private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount == 2)
            {
                Btnmax_Click(null, null);
            }
        }

        private void Grid_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                if (this.WindowState == System.Windows.WindowState.Maximized) // When maximized and windo is pinned (Win7,8,10)
                {
                    this.WindowState = System.Windows.WindowState.Normal;
                    var point = Mouse.GetPosition(Application.Current.MainWindow);
                    this.Left = point.X - this.Width / 2;
                    this.Top = point.Y - this.TitleBar.Height / 1;
                }
                this.DragMove();
            }
        }

        private void HalloText_TargetUpdated(object sender, DataTransferEventArgs e)
        {
        }

        private void MenuItem_Load_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = this.filterForFileDialog;

            if (ofd.ShowDialog() == true)
            {
                this.editor.Load(ofd.FileName);
            }
        }

        private void MenuItem_Save_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = this.filterForFileDialog;

            if (sfd.ShowDialog() == true)
            {
                this.editor.Save(sfd.FileName);
            }
        }

        #endregion Private Methods
    }
}